package com.rabbitmq.delayplugin.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    //延迟交换机
    public static final String DELAY_EXCHANGE_NAME="delay.exchange";

    //延迟队列
    public static final String DELAY_QUEUE_NAME="delay.queue";

    //延迟routingkey
    public static final String DELAY_QUEUE_ROUTINGKEY="delay.queue.routingkey";


    @Bean("delayQueue")
    public Queue delayQueue(){
        return new Queue(DELAY_QUEUE_NAME);
    }
    //插件实现声明延迟交换机
    @Bean("delayExchange")
    public CustomExchange delayExchange(){
        Map<String,Object> map=new HashMap<>();
        map.put("x-delayed-type","direct");
        return new CustomExchange(DELAY_EXCHANGE_NAME,"x-delayed-message",true,false,map);
    }
    @Bean
    public Binding bindingDelayQueue(@Qualifier("delayQueue")Queue queue,@Qualifier("delayExchange")CustomExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_ROUTINGKEY).noargs();
    }
}
